package com.cn.controller;
import com.cn.pojo.ShoppingCartItem;
import com.cn.pojo.User;
import com.cn.service.UserService;
import com.cn.utils.sendMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import java.util.List;

/**
 * @author:王宇航
 * @create: 2023-05-18 11:51
 * @Description:
 */

@RestController //该Controller全部返回JSON格式
@RequestMapping("/user")
@EnableWebMvc
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public User login(@RequestBody User user) {
        return userService.loginUser(user);
    }

    /*发送手机验证码*/
    @Autowired
    private sendMsg send;

    @RequestMapping(value = "/mobile", method = RequestMethod.POST)
    public int mobile(@RequestBody User user) {
        return send.send(user.getAccount());
    }

    /*查找手机号是否已存在*/
    @RequestMapping(value = "/searchMobile", method = RequestMethod.POST)
    public boolean searchMobile(@RequestBody User user) {
        if (userService.searchMobile(user) != null) {
            return true;
        } else
            return false;
    }

    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public boolean register(@RequestBody User user) {
        if (userService.insertUser(user) > 0) {
            return true;
        } else
            return false;
    }

    @RequestMapping(value = "/editInfo", method = RequestMethod.POST)
    public User editInfo(@RequestBody User user) {
        if (userService.editInfo(user) > 0) {
            return userService.searchByUserId(user);
        } else
            return null;
    }

    @RequestMapping(value = "/searchCartNumbers", method = RequestMethod.POST)
    public int searchCartNumbers(@RequestBody ShoppingCartItem cartItem) {
        return userService.searchCartNumbers(cartItem);
    }

    @RequestMapping(value = "/updateAddress", method = RequestMethod.POST)
    public void updateAddress(@RequestBody User user) {
       userService.updateAddress(user);
    }

    @RequestMapping(value = "/findAllUser", method = RequestMethod.POST)
    public List<User> findAllUser() {

        return  userService.findAllUser();
    }
    @RequestMapping(value = "/logout/{userId}", method = RequestMethod.POST)
    public void logout(@PathVariable  int userId) {
        userService.logout(userId);
    }
}
